<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>查看菜谱</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <#include "../../include/layui.ftl" />
</head>
<body>

  <div class="layui-fluid" id="content_div" style="text-align: center;">
      <h2 id="content_title"></h2>
      <div style="width: 100%;margin:10px 0px;" id="info"></div>
      <div >
          <table id="table_2" border="1" style="width: 100%;border:1px solid #091119;font-size: 12px;"></table>
      <div>
      <div style="page-break-before: always;">
          <table id="table_4" border="1" style="width: 100%;border:1px solid #091119;font-size: 12px;"></table>
      <div>

  </div>
  <script>
      layui.config({
          base: '${path}/plug-in/layuiadmin/' //静态资源所在路径
      }).extend({
          index: 'lib/index' //主入口模块
      }).use(['index','table','jquery'],function () {
          var  $ = layui.$,table = layui.table;
          var fun ={
              opt:{
                  url:"/console/checkNote/rewrite/getRecipes"
              },
              init:function(){
                  var index = layer.load(2, {shade: false}),_this = this;
                  $.getJSON(_this.opt.url,{checkId:parent.checkId},function(rep){
                      try {
                          if (!rep.flag) throw new DOMException("获取数据失败!");
                          _this.parseTableDom(rep.data);
                      } catch (e) {
                          layer.msg(e.message);
                      }finally {
                          layer.close(index);
                      }
                  });
              },
              parseTableDom:function(data){
                  var recipes = data.recipes;
                  var _this = this,html = [],len = recipes.length;
                  var title = "菜谱（"+recipes[0].startTime+" - "+recipes[len-1].endTime+"）";
                  $("#content_title").text(title);

                  _this.makeInfoData(data);
                  html.push('<tr style="height:25px;"><th style="width:15%;">时间</th><th style="width:20%;">早餐</th><th style="width:20%;">中餐</th><th style="width:20%;">晚餐</th><th style="width:20%;">加餐</th></tr>');
                  $.each(recipes,function(i,item){
                      html.push(_this.makeTableData(item.recipess));
                      if ((i+1) % 2 == 0) {
                          $('#table_'+(i+1)).html(html.join(""));
                          html = [];
                      }
                  });
              },
              makeInfoData:function(data){
                  var checkNote = data.checkNote,score = data.score,report = data.report,infoHtml = [];
                  var deliveryType = checkNote.deliveryType == 0? "顺产" : "剖腹产";
                  infoHtml.push("<table style='width: 100%;text-align: left;font-size:12px;' cellspacing='10' >");
                  infoHtml.push("<tr>");
                  infoHtml.push("<th style='text-align: right;width:60px;'>产妇姓名：</th>");
                  infoHtml.push("<td>"+checkNote.maternalName+"</td>");
                  infoHtml.push("<th style='text-align: right;width:60px;'>联系电话：</th>");
                  infoHtml.push("<td>"+checkNote.maternalMobile+"</td>");
                  infoHtml.push("<th style='text-align: right;width:60px;'>分娩日期：</th>");
                  infoHtml.push("<td>"+checkNote.deliveryDay.split(" ")[0]+"</td>");
                  infoHtml.push("<th style='text-align: right;width:60px;'>分娩方式：</th>");
                  infoHtml.push("<td>"+deliveryType+"</td>");
                  infoHtml.push("</tr>");

                  infoHtml.push("<tr>");
                  infoHtml.push("<th style='text-align: right;width:60px;'>体质：</th>");
                  infoHtml.push("<td>"+score.resultContent+"</td>");
                  infoHtml.push("<th style='text-align: right;width:60px;'>面诊：</th>");
                  infoHtml.push("<td>"+report.face.result.replace( new RegExp("#",'g'),"、")+"</td>");
                  infoHtml.push("<th style='text-align: right;width:60px;'>舌诊：</th>");
                  infoHtml.push("<td colspan='4'>"+report.tongue.result.replace(new RegExp("#",'g'),"、")+"</td>");
                  infoHtml.push("</tr>");

                  infoHtml.push("<tr>");
                  infoHtml.push("<th style='text-align: right;width:60px;'>忌吃：</th>");
                  infoHtml.push("<td>"+checkNote.avoidFood+"</td>");
                  infoHtml.push("<th style='text-align: right;width:60px;'>宜吃：</th>");
                  infoHtml.push("<td colspan='6'>"+score.advises+"</td>");
                  infoHtml.push("</tr>");
                  infoHtml.push("</table>");
                  $('#info').html(infoHtml.join(""));
              },
              makeTableData:function(recipess){
                  var tableData = [];
                  for (var j = 0;j < recipess.length; j++) {
                      var data = recipess[j];
                      var breakfast =  data.breakfast,lunch = data.lunch,
                              dinner = data.dinner,mealAddition = data.mealAddition;
                      var maxLen = Math.max.apply(null,[breakfast.length,lunch.length,dinner.length,mealAddition.length]);
                      var tr = [];
                      var td1 = [],td2 = [],td3 = [],td4 = [];
                      for (var i = 0;i < maxLen; i++) {
                          if (i < breakfast.length){
                              td1.push(breakfast[i].name);
                          }
                          if (i < lunch.length){
                              td2.push(lunch[i].name);
                          }
                          if (i < dinner.length){
                              td3.push(dinner[i].name);
                          }
                          if (i < mealAddition.length){
                              td4.push(mealAddition[i].name);
                          }
                      }
                      tr.push("<tr >");
                      tr.push("<td style='padding:3px;width: 15%;'>"+data.time+"</td>");
                      tr.push("<td style='padding:3px;width: 20%;'>"+td1.join("<br/>")+"</td>");
                      tr.push("<td style='padding:3px;width: 20%;'>"+td2.join("<br/>")+"</td>");
                      tr.push("<td style='padding:3px;width: 20%;'>"+td3.join("<br/>")+"</td>");
                      tr.push("<td style='padding:3px;width: 20%;'>"+td4.join("<br/>")+"</td>");
                      tr.push("</tr>");
                      tableData.push(tr);
                  }
                  return tableData;
              }
          };
          fun.init();
      });
  </script>

</body>
</html>
